var vo={};
var columns=[];
$(function(){
	var openWindow=window.document;
	var searchHeight=$("#divform").height();
	$("#listBack").height($(openWindow).height()-$(openWindow).height()*0.05-searchHeight);
	$("#listBack").datagrid({
					url:'/backcount/getcount',
					title:'未保存统计结果列表',
					method:'get',
					pagination:true,
					pageList: [10,20,50,200],
					loadMsg:'加载中......',
					singleSelect:true,
					nowrap:true,
					view: detailview,
					columns:[[ 
					        {field:'shopid',title:'门店编号', align:'left',width:100,sortable:true},
					        {field:'shopname',title:'门店名称', align:'left',width:100},
							{field:'dealerid',title:'经销商编号', align:'left',width:100,sortable:true},
							{field:'dealername',title:'经销商名称', align:'left',width:100},
							{field:'counttotal',title:'统计金额', align:'left',width:100},
							{field:'stand',title:'合格数量', align:'left',width:100,formatter:function(value,row,index){
								if(value==''||value==undefined){
									return 0;
								}
								return value;
							}},
							{field:'unstand',title:'不合格数量', align:'left',width:100,formatter:function(value,row,index){
								if(value==''||value==undefined){
									return 0;
								}
								return value;
							}},
							{field:'cmonth',title:'统计月份', align:'left',width:80},
							{field:'counttime',title:'统计时间', align:'left',width:130,sortable:true,formatter:function(value,row,index){
								 var datetime=Common.formatterDate2(value);
								 return datetime;
							}},
							{field:'countname',title:'统计人', align:'left',width:100},
					]],
					detailFormatter:function(index,row){
			            return '<div style="padding:2px"><table id="dayview' + index + '"></table></div>';
	                },
	                onExpandRow:function(index,row){
				    	 var shopid=row.shopid;
				    	 var startdate=row.startdate;
				    	 var enddate=row.enddate;
				    	 $("#dayview"+index).datagrid({
				    		 	url:'/backcount/getSub',
				    		 	queryParams:{'shopid':shopid,'startdate':startdate,'enddate':enddate},
								title:'每日统计',
								method:'get',
								pagination:true,
								pageList: window.subPageSize,
								loadMsg:'加载中......',
							    columns:[[
							              	 {field:'shopid',title:'门店编号', align:'left',width:100,sortable:true},
							              	 {field:'shopname',title:'门店名称', align:'left',width:100,sortable:true},
											 {field:'dealerid',title:'经销商编号', align:'left',width:100,sortable:true},
									         {field:'dealername',title:'经销商名称', align:'left',width:100,sortable:true},
									         {field:'price',title:'金额',align:'left',width:80},
									         {field:'stand',title:'合格数据',align:'left',width:80,formatter:function(value,row,index){
													if(value==''||value==undefined){
														return 0;
													}
													return value;
											 }},
									         {field:'unstand',title:'不合格数据',align:'left',width:80,formatter:function(value,row,index){
													if(value==''||value==undefined){
														return 0;
													}
													return value;
											 }},
									         {field:'checkdate',title:'日期',align:'left',width:80,}
									     ]],
							    onResize:function(){  
							    	$('#listBack').datagrid('fixDetailRowHeight',index);  
					             },
				                onLoadSuccess:function(){  
				                    setTimeout(function(){  
				                    	$('#listBack').datagrid('fixDetailRowHeight',index);  
				                    },0);  
				                },
				    	 });
				    	 $('#listBack').datagrid('fixDetailRowHeight',index);
				     }
				});
	var codelength=$('#cmonth').width();
	$('#cmonth').validatebox({
	    validType: 'month',
	    tipPosition:'top',
	    deltaX:codelength/4
	});
	//手动统计
	$("#serbutton").click(function(){
		var cmonth=$('#cmonth').val();
		if(cmonth==''||cmonth==undefined){
			$.messager.alert("失败","统计月份不能为空：",'error');
			return;
		}
		var reg=/^\d{4}-(0|1)\d{1}$/;
		if(!reg.test(cmonth)){
			$.messager.alert("失败","统计月份格式不正确应为：2016-01",'error');
			return;
		}
		$.messager.confirm('月末统计','您确定要统计'+$('#cmonth').val()+'月份数据吗？',function(data){
			if(data){
				$.ajax({
			         type: 'GET',
			         dataType: "json",
			         data:{'cmonth':cmonth},
			         url: '/backcount/handCount',
			         success: function (data) {
			        	 if(data.code==200){
								$.messager.alert("成功","统计成功",'info',function(){
									$("#listBack").datagrid('load',null);
								});
							}
							else{
								$.messager.alert("失败","统计失败："+data.msg,'error');
							}
						},
						error:function(data){
							$.messager.alert("失败","统计失败：",'error');
						}
			     })
			}
		})
	});
	$("#listBack").datagrid({
		toolbar: '#tb'
	});
	//月末统计
	$("#monthserbutton").click(function(){
		var date=new Date();
		var month=date.getMonth()+1;
		$.messager.confirm('月末统计','您确定要统计'+month+'月份数据吗？',function(data){
			if(data){
				var data=$("#listBack").datagrid('getData');
				if(data.rows.length>0){
					$.messager.alert("失败","存在未保存的统计记录：",'error');
					return;
				}
				$.ajax({
			         type: 'GET',
			         dataType: "json",
			         url: '/backcount/countMonth',
			         success: function (data) {
			        	 if(data.code==200){
								$.messager.alert("成功","统计成功",'info',function(){
									$("#listBack").datagrid('load',null);
								});
							}
							else{
								$.messager.alert("失败","统计失败："+data.msg,'error');
							}
						},
						error:function(data){
							$.messager.alert("失败","统计失败：",'error');
						}
			     })
			}
		})
	});
});
/**
 * 重新统计
 */
function reCount(){
	$.messager.confirm('月末统计','您确定要重新统计数据吗？',function(data){
		if(data){
			var data=$("#listBack").datagrid('getData');
			if(data.rows.length<=0){
				$.messager.alert("失败","不存在未保存的统计记录：",'error');
				return;
			}
			$.ajax({
		         type: 'GET',
		         dataType: "json",
		         url: '/backcount/reCount',
		         success: function (data) {
		        	 if(data.code==200){
							$.messager.alert("成功","统计成功",'info',function(){
								$("#listBack").datagrid('load',null);
							});
						}
						else{
							$.messager.alert("失败","统计失败："+data.msg,'error');
						}
					},
					error:function(data){
						$.messager.alert("失败","统计失败：",'error');
					}
		     })
		}
	})
}
/**
 * 放弃统计数据
 */
function delCount(){
	$.messager.confirm('月末统计','您确定要放弃统计数据吗？',function(data){
		if(data){
			var data=$("#listBack").datagrid('getData');
			if(data.rows.length<=0){
				$.messager.alert("失败","不存在未保存的统计记录：",'error');
				return;
			}
			$.ajax({
		         type: 'GET',
		         dataType: "json",
		         url: '/backcount/delCount',
		         success: function (data) {
		        	 if(data.code==200){
							$.messager.alert("成功","放弃成功",'info',function(){
								$("#listBack").datagrid('load',null);
							});
						}
						else{
							$.messager.alert("失败","放弃失败："+data.msg,'error');
						}
					},
					error:function(data){
						$.messager.alert("失败","放弃失败：",'error');
					}
		     })
		}
	})
}
/**
 * 保存统计数据
 */
function saveCount(){
	$.messager.confirm('月末统计','您确定要保存统计数据吗？',function(data){
		if(data){
			var data=$("#listBack").datagrid('getData');
			if(data.rows.length<=0){
				$.messager.alert("失败","不存在未保存的统计记录：",'error');
				return;
			}
			$.ajax({
		         type: 'GET',
		         dataType: "json",
		         url: '/backcount/saveCount',
		         success: function (data) {
		        	 if(data.code==200){
							$.messager.alert("成功","保存成功",'info',function(){
								$("#listBack").datagrid('load',null);
							});
						}
						else{
							$.messager.alert("失败","保存失败："+data.msg,'error');
						}
					},
					error:function(data){
						$.messager.alert("失败","保存失败：",'error');
					}
		     })
		}
	})
}